package com.aynu.admin.handler;

import cn.hutool.jwt.JWTException;
import com.aynu.admin.domain.CommonResponse;
import jakarta.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(JWTException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public CommonResponse<Object> handleException(JWTException e) {
        e.printStackTrace();
        return CommonResponse.failed(e.getMessage());
    }

    @ExceptionHandler({RuntimeException.class,ServletException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public CommonResponse<Object> handleRuntimeException(RuntimeException e) {
        e.printStackTrace();
        return CommonResponse.failed(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public CommonResponse<Object> handleException(Exception e) {
//        log.error(e.getMessage());
        e.printStackTrace();
        return CommonResponse.failed("服务器内部异常");
    }
}
